package com.wsoft.form.controller;

import com.github.pagehelper.PageInfo;
import com.wsoft.form.entity.WfFormEntity;
import com.wsoft.form.query.form.FormInsertQuery;
import com.wsoft.form.query.form.FormListQuery;
import com.wsoft.form.query.form.FormUpdateQuery;
import com.wsoft.form.service.IWfFormService;
import com.wsoft.response.ResponsePage;
import com.wsoft.response.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 自定义表单-页面表 前端控制器
 * </p>
 *
 * @author Z001
 * @since 2024年08月29日 09:11:06
 */
@RestController
@Tag(name = "页面表")
@RequestMapping("/wf/form")
public class WfFormController {

    @Resource
    private IWfFormService wfFormService;


    @Operation(summary = "新增页面", description = "新增页面")
    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody @Valid FormInsertQuery query) {
        boolean b = wfFormService.insertForm(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "编辑页面", description = "编辑页面")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody @Valid FormUpdateQuery query) {
        boolean b = wfFormService.updateForm(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "列表页面", description = "列表页面")
    @PostMapping("/list")
    public ResponseResult list(@RequestBody FormListQuery query) {
        PageInfo<WfFormEntity> page = wfFormService.listForm(query);
        return ResponseResult.success(ResponsePage.restPage(page));
    }

    @Operation(summary = "删除页面", description = "删除页面")
    @GetMapping("/delete")
    public ResponseResult delete(@RequestParam("id") Long id) {
        boolean b = wfFormService.delForm(id);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "详情页面", description = "详情页面")
    @GetMapping("/get")
    public ResponseResult get(@RequestParam("id") Long id) {
        return ResponseResult.success(wfFormService.getFormInfo(id));
    }
}
